﻿using HttpClient.HTTP;
using HttpClient.Models;
using HttpClient.Restful;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


/**
 * 需要安装：
 * Newtonsoft.Json
 */

namespace HttpClient
{
    public partial class Form1 : Form
    {
        // 自定义http请求对象
        private WebClient client;
        // 用户对象集合
        private List<User> users;

        private FrmUserEdit m_userEdit;



        public Form1()
        {
            InitializeComponent();
            client = new WebClient();
            users = new List<User>();
            m_userEdit = new FrmUserEdit();

            for (int i = 0; i < 3; i++)
            {
                dgvUser.Columns.Add(new DataGridViewTextBoxColumn());
            }

            dgvUser.Columns[0].Name = "ID";
            dgvUser.Columns[1].Name = "姓名";
            dgvUser.Columns[2].Name = "年龄";

            dgvUser.Columns[0].Width = 40;
            dgvUser.Columns[1].Width = 100;
            dgvUser.Columns[2].Width = 80;

            dgvUser.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            btnGet_Click(null, null);
        }



        private void btnInsert_Click(object sender, EventArgs e)
        {
            m_userEdit.Init(null, true);
            m_userEdit.ShowDialog();
            btnGet_Click(null, null);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            DataGridViewSelectedRowCollection rows = dgvUser.SelectedRows;
            if (rows.Count <= 0)
            {
                MessageBox.Show("未选中任何user！", "错误");
                return;
            }
            DataGridViewRow row = rows[0];
            User user = new User
            {
                Id = int.Parse(row.Cells[0].Value.ToString()),
                Name = row.Cells[1].Value.ToString(),
                Age = int.Parse(row.Cells[2].Value.ToString())
            };

            string url = "http://127.0.0.1:8087/test/user/delete";
            /*string body = new JavaScriptSerializer().Serialize(user);*/
            string body = JsonConvert.SerializeObject(user);
            string result = client.HttpPost(url, body);
            JsonResult jsonResult = JsonConvert.DeserializeObject<JsonResult>(result);
            if (jsonResult == null)
            {
                MessageBox.Show("请求异常！", "错误");
                return;
            }
            if (jsonResult.Code == 200)
            {
                MessageBox.Show("Success！", "提示");
                btnGet_Click(null, null);
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            DataGridViewSelectedRowCollection rows = dgvUser.SelectedRows;
            if (rows.Count <= 0)
            {
                MessageBox.Show("未选中任何user！", "错误");
                return;
            }
            DataGridViewRow row = rows[0];
            User user = new User
            {
                Id = int.Parse(row.Cells[0].Value.ToString()),
                Name = row.Cells[1].Value.ToString(),
                Age = int.Parse(row.Cells[2].Value.ToString())
            };

            m_userEdit.Init(user, false);
            m_userEdit.ShowDialog();
            btnGet_Click(null, null);
        }

        private void btnGet_Click(object sender, EventArgs e)
        {
            string url = "http://127.0.0.1:8087/test/user/get";
            string result = client.HttpGet(url);
            JsonResult jsonResult = JsonConvert.DeserializeObject<JsonResult>(result);
            if (jsonResult == null)
            {
                return;
            }
            if (jsonResult.Data == null)
            {
                return;
            }

            // 将请求结果的Data转换为User对象集合
            users = JsonConvert.DeserializeObject<List<User>>(jsonResult.Data.ToString());

            // 刷新用户列表
            ReflushUserList();
        }




        /// <summary>
        /// 刷新用户列表
        /// </summary>
        private void ReflushUserList()
        {
            try
            {
                dgvUser.Rows.Clear();
                for (int i = 0; i < users.Count; i++)
                {
                    dgvUser.Rows.Add();
                    dgvUser.Rows[i].Cells[0].Value = users[i].Id;
                    dgvUser.Rows[i].Cells[1].Value = users[i].Name;
                    dgvUser.Rows[i].Cells[2].Value = users[i].Age;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误");
            }
        }
    }
}
